<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="电话号码" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入电话号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="教师姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入教师姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button v-hasPermi="['cert:query']" type="primary" icon="el-icon-search" size="mini" @click="handleQuery">
          搜索
        </el-button>
        <el-button v-hasPermi="['cert:query']" icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="xueshiList">
      <el-table-column label="姓名" align="center" prop="name"/>
      <el-table-column label="证书名称" align="center" prop="type"/>
      <el-table-column label="证书编号" align="center" prop="code"/>
      <el-table-column label="电话号码" align="center" prop="phone"/>
      <el-table-column label="学校" align="center" prop="school"/>
      <el-table-column label="培训时间" align="center" prop="createDate">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-search"
            @click="handlePreview(scope.row)"
          >查看
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-download"
            @click="downloadCert(scope.row)"
          >下载
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { listXueshi, getXueshi, delXueshi, addXueshi, updateXueshi, preview } from '@/api/cert/cert'

export default {
  name: 'Cert',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 心理培训证书表格数据
      xueshiList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        phone: null,
        name: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        phone: [
          { required: true, message: '电话号码不能为空', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '教师姓名不能为空', trigger: 'blur' }
        ],
        teacherFlag: [
          { required: true, message: ' 是否心理任课教师不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询心理培训证书列表 */
    getList() {
      this.loading = true
      listXueshi(this.queryParams).then(response => {
        this.xueshiList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        phone: null,
        name: null,
        teacherFlag: null,
        code: null,
        type: null,
        date: null,
        section: null,
        province: null,
        city: null,
        county: null,
        school: null,
        classTime: null,
        stayTime: null,
        createDate: null,
        updateDate: null,
        remarks: null,
        delFlag: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加心理培训证书'
    },
    // 预览
     handlePreview(row) {
      const id = row.id
      const url = '/cert/xueshi/preview/' + id
     this.$download.priview(url).then(data=>{
       console.log(data)
       const newWindow = window.open()
       if (newWindow) {
         newWindow.document.write(`<img src="${data}" style="width: 100%; height: auto;">`)
         newWindow.document.close()
       } else {
         this.$msg('浏览器阻止了新窗口的打开')
       }
     })
    },
    // 下载证书
    downloadCert(row) {
      const id = row.id
      const url = '/cert/xueshi/preview/' + id
      this.$download.fetchFIle(url,row.code+".jpg")
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateXueshi(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addXueshi(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除心理培训证书编号为"' + ids + '"的数据项？').then(function() {
        return delXueshi(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('cert/cert/export', {
        ...this.queryParams
      }, `xueshi_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
